<?php
class diaocha extends base{

	public function init(){
		parent::init();
		$this->tit = '首页';

		$this->companyObj = D('company');
		$this->diaochaObj = D('company_diaocha');
		$this->diaocha_logObj = D('company_diaocha_log');
		$this->bingchuangObj = D('company_bingchuang');
		$this->zoneObj = D('company_zone');
		// $this->cateObj = D('cate');

		$this->adminObj = D('company_admin');
		$this->userObj = D('company_user');

		$this->sys_userObj = D('sys_user');

		$this->fankuiObj = D('company_fankui');
		$this->fankui_logObj = D('company_fankui_log');


		// $this->appid = 'wx913120c9717ff5d3';
    	// $this->appsecret = '8a284b6f7fbeaa91007f036363eae369';
		// $this->templateId = "CQ_3g4-qYAbB9_QiMniv5TfkAi0vw0pORCZtcf__YdU";

		$this->weixinObj = D('weixin');
		// $this->accessToken = $this->weixinObj->getAccessToken($this->appid, $this->appsecret);
	}

	public function index(){

	}

	public function ok(){

	}

	public function add1(){
		$bc_id = arg('bc_id');
		if(!$bc_id){
			echo 'bc_id';
			exit;
		}
		$bingchuang = $this->bingchuangObj->findById($bc_id);
		$zone = $this->zoneObj->findById($bingchuang['zone_id']);
		$company = $this->companyObj->findById($bingchuang['company_id']);

		if($this->fmdo == 'add'){
			$count = arg('count');
			$log = arg('log');
			$logcount = count($log);
			if($count != $logcount){
				$this->renderError('请完成全部选项');
			}

			$bc_id = arg('bc_id');
			$bingchuang = $this->bingchuangObj->findById($bc_id);
            if(!$this->user_id){
                $this->user_id = 0;
            }



			// 不满意反馈
			if($log){
				foreach($log as $val){
					// 1,一般满意
					$needle = "不满意";
					if (strpos($val, $needle) !== false) {
						// echo "找到了";
						$arr = explode(',',$val);
						$diaocha_id = $arr[0];

						$dcinfo = $this->diaochaObj->findById($diaocha_id);
						if($dcinfo){
							if($dcinfo['admin_ids']){
								$admins = explode(',',$dcinfo['admin_ids']);
								foreach($admins as $val){
									$arrBase = array(
										'company_id' =>$bingchuang['company_id'],
										'zone_id' =>$bingchuang['zone_id'],
										'bingchuang_id' =>$bingchuang['id'],
										'diaocha_id' =>$diaocha_id,
										'admin_id' =>$val,
										'user_id' =>$this->admin_id,
										'intro'=>arg('intro'),
										'status'=>1,
										'add_time' =>date('Y-m-d H:i:s'),
									);
									$this->fankuiObj->add($arrBase);
								}
							}
						}
					}
				}
			}



			$arrBase = array(
				'user_id' =>$this->user_id,
				// 'user_name' =>arg('user_name'),
				// 'user_tel' =>arg('user_tel'),
				'company_id' =>$bingchuang['company_id'],
				'zone_id' =>$bingchuang['zone_id'],
				'bingchuang_id' =>$bingchuang['id'],
				'log'=>joinStr($log,'|'),
				'intro'=>arg('intro'),
				// 'tel'=>arg('tel'),
				'status'=>1,
				'add_time' =>date('Y-m-d H:i:s'),
			);
			$this->diaocha_logObj->add($arrBase);

			// $this->ajaxMsg('提交成功',1,url('diaocha','ok'));
			$this->renderSuccess('提交成功','/diaocha/ok');
		}

		//系统
		// $cond = "status=1 and company_id='{$bingchuang['company_id']}' ";
		// $res = $this->diaochaObj->findAll($cond,'sort desc,id asc');

		if($bingchuang){
			//医院
			$cond = "status=1 and company_id='{$bingchuang['company_id']}' ";
			$res = $this->diaochaObj->findAll($cond,'sort desc,id asc');
			// $res = array_merge($res,$resyy);

			if($res){
				$count = count($res);
				foreach($res as $val){
					$val['options'] = explode('|', $val['options']);
					$list[] = $val;
				}
				$this->list = $list;
			}
		}
		// dump($list);exit;
		$this->count = $count;
		$this->bingchuang = $bingchuang;
		$this->zone = $zone;
		$this->company = $company;
	}


	// 调查
	public function add(){
		$bc_id = arg('bc_id');
		if(!$bc_id){
			echo '请扫病床二维码提交调查。';
			exit;
		}



		if($this->fmdo == 'add'){
			$submittedToken = arg('submit_token');
			$storedToken = $_SESSION['submit_token'];
			// dump($submittedToken);
			// dump($storedToken);
			if ($submittedToken === $storedToken) {
				// 执行表单提交操作
				unset($_SESSION['submit_token']); // 清除 token
			}else{
				$this->renderError('请勿重复提交');
			}

			$intro = arg('intro');
			$count = arg('count');
			$ids = arg('ids');
			$log = arg('log');
			$log2 = arg('log2');

			$logcount = count($log);
			if($count != $logcount){

				$log_ids = explode(',',$ids);
				if($log){
					$err = "";
					foreach($log_ids as $val){
						if(!$log["A{$val}"]){
							$err = "{$val}";
							break;
						}
					}
					$url = "#A{$err}";
				}else{
					$url = "#A{$log_ids[0]}";
				}
				$this->renderError('请完成全部选项',$url);
			}
			$bc_id = arg('bc_id');
			$bingchuang = $this->bingchuangObj->findById($bc_id);
            if(!$this->user_id){
                $this->user_id = 0;
            }



			// dump(joinStr($log,'|'));exit;

			$arrBase = array(
				'user_id' =>$this->user_id,
				// 'user_name' =>arg('user_name'),
				// 'user_tel' =>arg('user_tel'),
				'company_id' =>$bingchuang['company_id'],
				'zone_id' =>$bingchuang['zone_id'],
				'bingchuang_id' =>$bingchuang['id'],
				'log'=>joinStr($log,'|'),
				'intro'=> $intro,
				'count_num' =>$count,
				'status'=>1,
				'add_time' =>date('Y-m-d H:i:s'),
			);
			$log_id = $this->diaocha_logObj->add($arrBase);
			if($log_id){
				// 不满意反馈
				if($log){
					$zscore = 0;
					foreach($log as $key => $val){



						$arr = explode(',',$val);
						$diaocha_id = $arr[0];
						$options = $arr[1];

						// 存在不满意  1,不满意  2 是
						$needle = "不满意";
						$needle2 = "是";

						$dcinfo = $this->diaochaObj->findById($diaocha_id);
						if($dcinfo){
							if($dcinfo['is_manyi']==1){
								$needle2 = "否";
							}
						}

						if (strpos($val, $needle) !== false  || strpos($val, $needle2) !== false) {

							$arr = explode(',',$val);
							$diaocha_id = $arr[0];
							$options = $arr[1];

							$dcinfo = $this->diaochaObj->findById($diaocha_id);
							if($dcinfo){
								// 提交反馈
								$order_status = 1;
								$arrBase = array(
									'company_id' =>$bingchuang['company_id'],
									'zone_id' =>$bingchuang['zone_id'],
									'bingchuang_id' =>$bingchuang['id'],
									'diaocha_id' =>$diaocha_id,
									'diaocha_log_id' =>$log_id,
									'title' =>$dcinfo['title'],
									'options' =>$options,
									'shifu_id' =>0,
									'user_id' =>$this->user_id,
									'intro'=> $log2[$key],
									'status'=>1,
									'order_status'=>$order_status,
									'add_time' =>date('Y-m-d H:i:s'),
									'up_time' =>date('Y-m-d H:i:s'),
								);
								$fk_id = $this->fankuiObj->add($arrBase);
								if($fk_id){
									//log 新反馈
									$data = [];
									$this->fankui_logObj->add_log($fk_id,$data);

									$admin_arr = [];
									$shifu = [];
									// 存在绑定调查的维护
									if($dcinfo['admin_ids']){
										// 存在绑定调查的师傅
										$cond = "status=1 and (level=3) " .$this->cond_base;
										$cond .= " and is_sign=1 and id IN ({$dcinfo['admin_ids']})";
										$shifu = $this->adminObj->find(array($cond),'id asc');
										if($shifu){
											$admin_arr[] = $shifu['id'];
											// 师傅发送模版消息
											$msg = "存在不满意选项，请及时维护。";
											$this->fankuiObj->send_weixin_msg($this->accessToken,$shifu,$fk_id,$msg);
										}
									}

									$shifu = [];
									// 是否科室问题 发送给护士长或科室主任
									if($dcinfo['is_keshi']){
										// 存在绑定调查的师傅
										$cond = "status=1 and (level=5 or level=6) " .$this->cond_base;
										$cond .= " and is_sign=1 and id IN ({$dcinfo['admin_ids']})";
										$cond .= " and find_in_set('{$bingchuang['zone_id']}',zone_ids)";
										$shifu = $this->adminObj->find(array($cond),'id asc');

										if($shifu){
											$admin_arr[] = $shifu['id'];
											// 师傅发送模版消息
											$msg = "存在不满意选项，请及时维护。";
											$this->fankuiObj->send_weixin_msg($this->accessToken,$shifu,$fk_id,$msg);
										}
									}

									if($admin_arr){

										$admin_ids = implode(',',$admin_arr);
										// $admin_ids2 = implode(',',$admin_arr2);
										// 更新师傅
										$order_status = 2;
										$arrBase = array(
											// 'shifu_id' =>$admin_ids1,
											'admin_ids' =>$admin_ids,
											'order_status'=>$order_status,
											'up_time' =>date('Y-m-d H:i:s'),
										);
										$this->fankuiObj->edit($fk_id,$arrBase);
										//log  已派单
										$data = [];
										$this->fankui_logObj->add_log($fk_id,$data);

									}

								}

							}

							$log_bmy[] = $val;
						}

						// 得分
						$score = $this->diaochaObj->get_score($val);
						if($score>=0){
							// 有效得分
							$score_arr[] = $diaocha_id;
							$zscore = $zscore + $score;
						}
						$score_log[] = $diaocha_id.','.$score;
					}
					// 有效平均分
					$score_logcount = count($score_arr);
					$score_pj = round($zscore / $score_logcount , 2);
					// 平均分
					// $score_pj = round($zscore / $logcount , 2);

					// 更新不满意 得分
					$count_bmy = count($log_bmy);
					$arrBase = array(
						'log_bmy' => joinStr($log_bmy,'|'),
						'count_bmy' =>$count_bmy,
						'score_log' => joinStr($score_log,'|'),
						'score' => $score_pj,
					);
					$this->diaocha_logObj->edit($log_id,$arrBase);
				}


				// 存在建议
				if($intro){

					$order_status = 1;
					$arrBase = array(
						'company_id' =>$bingchuang['company_id'],
						'zone_id' =>$bingchuang['zone_id'],
						'bingchuang_id' =>$bingchuang['id'],
						// 'diaocha_id' =>$diaocha_id,
						'diaocha_log_id' =>$log_id,
						'title' =>'具体意见或建议',
						// 'options' =>$options,
						'shifu_id' =>0,
						'user_id' =>$this->user_id,
						'intro'=> $intro,
						'status'=>1,
						'order_status'=>$order_status,
						'add_time' =>date('Y-m-d H:i:s'),
						'up_time' =>date('Y-m-d H:i:s'),
					);
					$fk_id = $this->fankuiObj->add($arrBase);
					if($fk_id){
						//log
						$data = [];
						$this->fankui_logObj->add_log($fk_id,$data);



						$shifu = [];
						// 存在绑定调查的师傅
						$cond = "status=1 and (level=3)" .$this->cond_base;
						$cond .= " and is_sign=1 and find_in_set('-100',diaocha_ids)";
						$shifu = $this->adminObj->find(array($cond),'id asc');
						if($shifu){

							$order_status = 2;
							$arrBase = array(
								'shifu_id' =>$shifu['id'],
								'admin_ids' =>$shifu['id'],
								'order_status'=>$order_status,
								'up_time' =>date('Y-m-d H:i:s'),
							);
							$this->fankuiObj->edit($fk_id,$arrBase);
							//log
							$data = [];
							$this->fankui_logObj->add_log($fk_id,$data);


							// 师傅发送模版消息
							$msg = "存在意见或建议，请及时维护。";
							$this->fankuiObj->send_weixin_msg($this->accessToken,$shifu,$fk_id,$msg);

						}
					}

				}
				//
			}

			// 发送模版消息
			$messdata = array(
				'accessToken' =>$this->accessToken,
				'openId' =>$this->sys_user['open_id'],
				'templateId' =>$this->templateId,
				'first' =>"Log-{$log_id}",
				'keyword1' =>"您的满意度调查已收到，感谢您的反馈。",
				'keyword2' =>date("Y-m-d H:i:s"),
			);
			$url = "https://t.20m.cn/my/index";
			$res = $this->weixinObj->sendWxMessage($messdata,$url);



			// $this->ajaxMsg('提交成功',1,url('diaocha','ok'));
			$this->renderSuccess('提交成功','/diaocha/ok');
		}else{
			$submit_token = md5(uniqid());
			if($submit_token){
				$this->submit_token = $submit_token;
				$_SESSION['submit_token'] = $submit_token;
			}
		}


		$bingchuang = $this->bingchuangObj->findById($bc_id);
		$zone = $this->zoneObj->findById($bingchuang['zone_id']);
		$company = $this->companyObj->findById($bingchuang['company_id']);
		// if($company){
		// 	$_SESSION["company_id"] = $company['id'];
		// }
		//系统
		// $cond = "status=1 and is_sys=1 company_id='{$bingchuang['company_id']}' ";
		// $res = $this->diaochaObj->findAll($cond,'sort desc,id asc');

		if($bingchuang){
			//医院
			$cond = "status=1 and company_id='{$bingchuang['company_id']}' ";
			$res = $this->diaochaObj->findAll(array($cond),'sort asc,id asc');

			// $res = array_merge($res,$resyy);

			if($res){
				$count = count($res);
				foreach($res as $key => $val){
					$ids[] = $val['id'];
					$val['i'] = $key + 1;
					$val['options'] = explode('|', $val['options']);
					$list[] = $val;
				}
				$this->ids = implode(',',$ids);
				$this->list = $list;
			}
		}


		// dump($list);exit;
		$this->count = $count;
		$this->bingchuang = $bingchuang;
		$this->zone = $zone;
		$this->company = $company;
	}




}